package controllers

import (
	"ceshi2/cache"
	"ceshi2/models"
	"ceshi2/pkg"
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"path/filepath"
	"time"
)

type MainControll struct {
	beego.Controller
}
type PageData struct {
	Code int64       `json:"code"`
	Data interface{} `json:"data"`
}
type Res struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *MainControll) Register() {
	username := c.GetString("username")
	password := c.GetString("password")

	var user = models.User{
		Username: username,
		Password: pkg.Md5(password),
	}

	if username == "" || password == "" {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "账号或密码不能为空",
			Data: nil,
		}
		c.ServeJSON()
		return
	}

	err := models.DB.Where("username=?", username).Find(&user).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "系统报错",
			Data: nil,
		}
		c.ServeJSON()
		return
	}

	if user.ID != 0 {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "用户名已存在",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	err = models.DB.Create(&user).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "注册失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "注册成功",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
}

func (c *MainControll) Login() {
	username := c.GetString("username")
	password := c.GetString("password")

	var user = models.User{
		Username: username,
		Password: pkg.Md5(password),
	}

	if username == "" || password == "" {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "账号或密码不能为空",
			Data: nil,
		}
		c.ServeJSON()
		return
	}

	err := models.DB.Where("username=?", username).Find(&user).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "系统报错",
			Data: nil,
		}
		c.ServeJSON()
		return
	}

	if user.ID == 0 {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "用户名不存在",
			Data: nil,
		}
		c.ServeJSON()
		return
	}

	key := "user_login" + username
	count, _ := cache.Rdb.Get(cache.Ctx, key).Int()
	if count >= 3 {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "密码错误3次,封禁2小时",
			Data: nil,
		}
		c.ServeJSON()
		return
	}

	if user.Password != pkg.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, key).Result()
		if count == 2 {
			cache.Rdb.Expire(cache.Ctx, key, time.Hour*2).Result()
		}
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "密码错误",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "登录成功",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
}

func (c *MainControll) Add() {
	name := c.GetString("name")
	status := c.GetString("status")

	var dog = models.Dog{
		Name:   name,
		Status: status,
	}
	err := models.DB.Create(&dog).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
}

func (c *MainControll) Upload() {
	file, header, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "文件获取失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	ext := filepath.Ext(header.Filename)
	if ext != ".jpg" && ext != ".png" {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "文件只能是jpg或者png",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	defer file.Close()
	if header.Size > 2*1024*1024 {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "文件不能大于2mb",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	path := "static/img/" + header.Filename
	err = c.SaveToFile("img", path)
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "上传失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		Url := "http://127.0.0.1:8080/" + path
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "上传成功",
			Data: Url,
		}
		c.ServeJSON()
		return
	}
}

func (c *MainControll) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	var dog []models.Dog
	err := models.DB.Find(&dog).Error

	key := "dog_list"
	res, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if res != "" {
		json.Unmarshal([]byte(res), &dog)
	} else {
		jsonData, _ := json.Marshal(dog)
		cache.Rdb.Set(cache.Ctx, key, jsonData, time.Hour*3)
	}

	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "查询成功",
			Data: dog,
		}
		c.ServeJSON()
		return
	}
}

func (c *MainControll) First() {
	var dog []models.Dog
	id, _ := c.GetInt("id")
	err := models.DB.Where("id=?", id).Find(&dog).Error

	key := "dog_first"
	res, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if res != "" {
		json.Unmarshal([]byte(res), &dog)
	} else {
		jsonData, _ := json.Marshal(dog)
		cache.Rdb.Set(cache.Ctx, key, jsonData, time.Hour*2).Result()
	}

	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "详情失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "详情成功",
			Data: dog,
		}
		c.ServeJSON()
		return
	}
}

func (c *MainControll) Del() {
	id, _ := c.GetInt("id")
	var people []models.People
	err := models.DB.Delete(&people, id).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "删除失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "删除成功",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
}

func (c *MainControll) Limit() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 3)
	i := (page - 1) * size
	var count int64
	var people []models.People
	models.DB.Offset(i).Limit(size).Find(&people)
	models.DB.Model(&people).Count(&count)
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "查询成功",
		Data: PageData{
			Code: count,
			Data: people,
		},
	}
	c.ServeJSON()
	return
}
